home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / Collections / WordCollection.java < prev   
Encoding:
Java Source  |  1997-02-27  |  2.8 KB  |  122 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7.  
  8.  
  9. public class wordcollection extends chunkedtext {
  10.  
  11.     /**
  12.      ** Constructors
  13.      **/
  14.      public wordcollection(StringBuffer inString){
  15.          super(inString);
  16.      }
  17.      
  18.      public wordcollection(String inString){
  19.          super(inString);
  20.      }
  21.          
  22.     
  23.     /** 
  24.      ** Inherites the bulk of collection protocol from chunkedtext
  25.      **/
  26.     
  27.     
  28.     /** 
  29.      ** protected handlers
  30.      **/
  31.     
  32.     protected boolean isChunkSeparator(char inCh){
  33.         boolean found = false;
  34.         for(int i = 0; (! found) && (i < mWordDelimiters.length()); i++ ){
  35.             found = (inCh == mWordDelimiters.charAt(i));
  36.         }
  37.         return found;
  38.     }
  39.     
  40.     protected char defaultChunkSeparator(){
  41.         return ' ';
  42.     }
  43.  
  44.     
  45.     /** 
  46.      ** private parts
  47.      **/
  48.      
  49.     private static final String  mWordDelimiters = " .,–?!:;\n\t\\";
  50.     private static final char     mThousandsSeparator = ',';
  51.     private static final char     mDecimalPoint = '.';
  52.  
  53.     
  54.     protected int[] nextChunk(int inLookingFrom) {
  55.         // retruns an array of 2 integers, start and finish
  56.         // inLookingFrom is the character index where we start looking
  57.         
  58.         int wordStart = stringLength(); //default vals
  59.         int wordEnd = stringLength(); //default vals
  60.         int i = inLookingFrom;
  61.         
  62.         if (inLookingFrom <= 1){ //if it's the very first time
  63.             wordStart = 0;
  64.         } else {
  65.             for (; i < stringLength(); i++){
  66.                 //go untill we find a non-delimiter
  67.                 if (! (isChunkSeparator(getNthChar(i)))){
  68.                     wordStart = i;
  69.                     break;
  70.                 }
  71.             }
  72.         }
  73.         for (; i < stringLength(); i++){
  74.             //go untill we find a delimiter 
  75.             //(unless its a thousands Separator or decimel point)
  76.             if (isChunkSeparator(getNthChar(i)) 
  77.                 && (! isDecimalPoint(i))
  78.                 && (! isThousandsSeparator(i))) {
  79.                     wordEnd = i;
  80.                     break;
  81.             }
  82.         }
  83.         int returnVal[] = {wordStart, wordEnd};
  84.         return returnVal;
  85.     }
  86.  
  87.     
  88.     
  89.     private boolean isNumeral(char inCh){
  90.         return ((inCh < '9') && (inCh > '0'));
  91.     }
  92.     
  93.     
  94.     private boolean isThousandsSeparator (int inCharacterIndex) {
  95.         boolean returnVal = false;
  96.         try {
  97.             returnVal = ((mThousandsSeparator == getNthChar(inCharacterIndex))
  98.                 && (inCharacterIndex + 3 <= stringLength())
  99.                 && (isNumeral( getNthChar(++ inCharacterIndex)))
  100.                 && (isNumeral( getNthChar(++ inCharacterIndex)))
  101.                 && (isNumeral( getNthChar(++ inCharacterIndex))));
  102.         }
  103.         catch (RuntimeException e){
  104.              returnVal = false; 
  105.          }
  106.          return returnVal;
  107.     }
  108.     
  109.     
  110.     private boolean isDecimalPoint (int inCharacterIndex) {
  111.         boolean returnVal = false;
  112.         try {
  113.             returnVal = ((mDecimalPoint == getNthChar(inCharacterIndex))
  114.                 && (inCharacterIndex + 1 <= stringLength())
  115.                 && (isNumeral( getNthChar(++ inCharacterIndex))));
  116.         }
  117.         catch (RuntimeException e){
  118.              returnVal = false; 
  119.          }
  120.          return returnVal;
  121.     }
  122. }